package animation.sort.gui;

/**
 * Created by IntelliJ IDEA.
 * User: emilio
 * Date: Aug 10, 2004
 * Time: 9:35:21 PM
 * To change this template use File | Settings | File Templates.
 */

import java.awt.*;
import javax.swing.*;

import animation.sort.SortAnimation;

public class Main
{
    JPanel mainPanel;
    AnimationDisplayArea[] displayArea;
    private SortToolBar toolBar;

    public Main(int panels)
    {
        displayArea = new AnimationDisplayArea[panels];
        JComboBox algorithmSelector = createAlgorithmSelector();
        toolBar = new SortToolBar(algorithmSelector);
        MonitoringArea monitoringArea = new MonitoringArea();
        displayArea[0] = new AnimationDisplayArea(this, toolBar, algorithmSelector, panels, monitoringArea);

        //Create the main panel to contain the two sub panels.
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        //Add the select and display panels to the main panel.
        mainPanel.add(toolBar);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        mainPanel.add(monitoringArea);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        mainPanel.add(displayArea[0]);

        if (panels == 2) {
            mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
            JPanel auxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            algorithmSelector = createAlgorithmSelector();
            auxPanel.add(algorithmSelector);
            mainPanel.add(auxPanel);
            mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
            monitoringArea = new MonitoringArea();
            mainPanel.add(monitoringArea);
            mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
            displayArea[1] = new AnimationDisplayArea(this, toolBar, algorithmSelector, panels, monitoringArea);
            mainPanel.add(displayArea[1]);
        }
    }

    public void show()
    {
        //Create and set up the window.
        JFrame jframe = new JFrame("Sort");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setContentPane(mainPanel);

        //Display the window.
        jframe.pack();
        for (int i = 0; i < displayArea.length; i++) {
            displayArea[i].initAnimation();

        }
        jframe.setVisible(true);
    }

    public static void main(String[] args)
    {
        final int panels = args.length > 0 && args[0].equals("-2") ? 2 : 1;

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                Main main = new Main(panels);
                main.show();
            }
        });
    }


    static JComboBox createAlgorithmSelector()
    {
        final Object[] algorithms = SortAnimation.getAlgorithms();
        JComboBox sortChoices = new JComboBox(algorithms);
        sortChoices.setMaximumRowCount(algorithms.length);
        sortChoices.setMaximumSize(sortChoices.getPreferredSize());
        return sortChoices;
    }

    public boolean anyRunning()
    {
        for (int i = 0; i < displayArea.length; i++) {
            if (displayArea[i].isRunning())
                return true;
        }
        return false;
    }
}

